package com.jswlw.common.utils.crc16;

public class HexByteUtil {

    // 二进制转字符串
    public static String byte2hex(byte[] b) {
        StringBuffer sb = new StringBuffer();
        String tmp = "";
        for (int i = 0; i < b.length; i++) {
            tmp = Integer.toHexString(b[i] & 0XFF);
            if (tmp.length() == 1) {
                sb.append("0" + tmp);
            } else {
                sb.append(tmp);
            }

        }
        return sb.toString();
    }

    // 字符串转二进制
    public static byte[] hex2byte(String str) {
      if (str == null){  
       return null;  
      }  
        
      str = str.trim();  
      int len = str.length();  
        
      if (len == 0 || len % 2 == 1){  
       return null;  
      }  
        
      byte[] b = new byte[len / 2];  
      try {  
           for (int i = 0; i < str.length(); i += 2) {
                b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();  
           }
           return b;  
      } catch (Exception e) {
       return null;  
      }  
    }

    public static String kgstr(byte[] bytes) {
        String hex = new String(bytes);
        String ss = "";
        int z = 0;
        for (int i = 0; i < hex.length() / 2; i++) {
            String a = hex.substring(z,z+2);
            ss = ss + a + " ";
            z = z + 2;
        }
        return ss;
    }

    public static Integer sixToten(byte[] bytes) {
        String hex = new String(bytes);
        hex = hex.replace(" ", "");
        Integer x = Integer.parseInt(hex.substring(2),16); //从第2个字符开始截取
        return x;
    }
    
    //将字符串每两位直接加上空格
    public static String kgstr(String hex){
    	String ss="";
    	  int z = 0;
    	 for(int i=0;i<hex.length()/2;i++){
    		   String a = hex.substring(z,z+2);
    		   ss=ss+a+" ";
    		   z = z+2;
    		  }
    	return ss;
    }
  //16进制转10进制   两种情况 fff;  0xfff； 
  	public static Integer sixToten(String hex){
  		hex=hex.replace(" ", "");
  		Integer x = Integer.parseInt(hex.substring(2),16);//从第2个字符开始截取
  		return x;
  	}
  	
  	public static String tenTosix(int num,int length){
  		String s=Integer.toHexString(num);
  		if(s.length()<length){
  			int a =length-s.length();
  			for(int i=0;i<a;i++){
  				s="0"+s;
  			}
  		}
  		return s;
  	}
  	
	/**
  	 * 16进制的字符串表示转成字节数组
  	 *
  	 * @param hexString 16进制格式的字符串            
  	 * @return 转换后的字节数组
  	 **/
  	public static byte[] toByteArray(String hexString, boolean isBigEndian) {
  	    hexString = hexString.toLowerCase();
  	    final byte[] byteArray = new byte[hexString.length() / 2];
  	    int k = 0;
  	    for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
			byte high;
			byte low;
  	    	if (isBigEndian) {
				high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
				low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
			} else {
				high = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
				low = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
			}
  	        byteArray[i] = (byte) (high << 4 | low);
  	        k += 2;
  	    }
  	    return byteArray;
  	}
  	/**
  	 * byte[]数组转换为16进制的字符串
  	 *
  	 * @param data 要转换的字节数组
  	 * @return 转换后的结果
  	 */
  	public static final String byteArrayToHexString(byte[] data) {
  	    StringBuilder sb = new StringBuilder(data.length * 2);
  	    for (byte b : data) {
  	        int v = b & 0xff;
  	        if (v < 16) {
  	            sb.append('0');
  	        }
  	        sb.append(Integer.toHexString(v));
  	    }
  	    return sb.toString().toUpperCase();
  	}
  	/**
  	 * byte数组转换为二进制字符串,每个字节以","隔开
  	 **/
  	public static String byteArrToBinStr(byte[] b) {
  	    StringBuffer result = new StringBuffer();
  	    for (int i = 0; i < b.length; i++) {
  	        result.append(Long.toString(b[i] & 0xff, 2) + ",");
  	    }
  	    return result.toString().substring(0, result.length() - 1);
  	}

  	/**
  	 * 二进制字符串转换为byte数组,每个字节以","隔开
  	 **/
  	public static byte[] binStrToByteArr(String binStr) {
  	    String[] temp = binStr.split(",");
  	    byte[] b = new byte[temp.length];
  	    for (int i = 0; i < b.length; i++) {
  	        b[i] = Long.valueOf(temp[i], 2).byteValue();
  	    }
  	    return b;
  	}
    public static void main(String[] args) {
    	  String hex16String = "898602b1031700117184";
          //引用。
          byte[] bytes = toByteArray(hex16String, true);
          System.out.println(bytes);
          System.out.println(byteArrayToHexString(bytes));
          System.out.println(HexByteUtil.sixToten("00 17"));
	}
}
